package cn.edu.scau.vsac.controller;

import cn.edu.scau.vsac.common.Constants;
import cn.edu.scau.vsac.common.ResultData;
import cn.edu.scau.vsac.domain.dto.HelpOfferDto;
import cn.edu.scau.vsac.entity.HelpOffer;
import cn.edu.scau.vsac.service.HelpOfferService;
import cn.edu.scau.vsac.util.HostHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "帮扶")
@RequestMapping("HelpOffer")
public class HelpOfferController {
    @Autowired
    HelpOfferService helpOfferService;
    @Autowired
    HostHolder hostHolder;
    @Operation(summary = "新增帮扶")
    @RequestMapping(path = "/create", method = RequestMethod.POST)
    public ResultData<HelpOffer> createHelp(@RequestBody HelpOfferDto helpOfferDto) {
        HelpOffer helpOffer = helpOfferService.create(helpOfferDto);
        if (helpOffer == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(helpOffer);
    }

    @Operation(summary = "删除帮扶")
    @RequestMapping(path = "/delete/{id}", method = RequestMethod.POST)
    public ResultData<String> deleteRequestHelp(@PathVariable Long id) {
        String delete = helpOfferService.deleteById(id);
        if (delete == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(delete);
    }

    @Operation(summary = "根据主键查询帮扶")
    @RequestMapping(path = "/getOneById/{id}", method = RequestMethod.GET)
    public ResultData<HelpOffer> getById(@PathVariable Long id) {
        HelpOffer helpOffer = helpOfferService.getOneById(id);
        if (helpOffer == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(helpOffer);
    }

    @Operation(summary = "根据帮扶人id查询帮扶请求", description = "传入用户id和分页信息 根据求助人id查询帮扶")
    @RequestMapping(path = "/getPageByHelperId", method = RequestMethod.GET)
    public ResultData<List<HelpOffer>> getPageByHelperId( ) {
        Long helperId =  hostHolder.getUser().getUserId();
        List<HelpOffer> res = helpOfferService.getPageByHelperId(helperId);
        if (res == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(res);
    }

    @Operation(summary = "根据求助表主键id查询帮扶请求", description = "传入求助表主键id和分页信息 根据求助表主键id查询帮扶")
    @RequestMapping(path = "/getByRequestId/{requestId}", method = RequestMethod.GET)
    public ResultData<HelpOffer> getByRequestId(@PathVariable Long requestId) {
        HelpOffer res = helpOfferService.getByRequestId(requestId);
        if (res == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(res);
    }
}
